﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.Objects;
using System.Data.Entity.Infrastructure;

namespace CustomerService
{
    public partial class EditCustomer : System.Web.UI.Page
    {
        private SalesManagerEntities _sme;
        private tblCustomer _customer = null;
        private int _customerID;

        protected void Page_Load(object sender, EventArgs e)
        {
            _sme = (SalesManagerEntities)Session["sme"];
            int.TryParse(Request.QueryString["CustomerID"], out _customerID);
            if (!IsPostBack)
            {
                SqlConnection conn = (SqlConnection)_sme.Database.Connection;
                if (conn.State != System.Data.ConnectionState.Open )
                    conn.Open();
                SqlCommand cmm = conn.CreateCommand();

                cmm.CommandText = "exec spLoadCustomer @CustomerID";
                cmm.Parameters.Add(new SqlParameter("@CustomerID", _customerID));
                SqlDataReader rdr = cmm.ExecuteReader();

                ((IObjectContextAdapter)_sme).ObjectContext.Translate<tblAddress>(rdr, "SalesManagerEntities.tblAddress", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                ((IObjectContextAdapter)_sme).ObjectContext.Translate<tblPerson>(rdr, "SalesManagerEntities.tblPerson", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                ((IObjectContextAdapter)_sme).ObjectContext.Translate<codeContactPointType>(rdr, "SalesManagerEntities.codeContactPointType", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                _customer = ((IObjectContextAdapter)_sme).ObjectContext.Translate<tblCustomer>(rdr, "SalesManagerEntities.tblCustomer", MergeOption.AppendOnly).FirstOrDefault();
                rdr.NextResult();
                ((IObjectContextAdapter)_sme).ObjectContext.Translate<tblContactPoint>(rdr, "SalesManagerEntities.tblContactPoint", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                ((IObjectContextAdapter)_sme).ObjectContext.Translate<codeHoldReason>(rdr, "SalesManagerEntities.codeHoldReason", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                ((IObjectContextAdapter)_sme).ObjectContext.Translate<codeOrderSource>(rdr, "SalesManagerEntities.codeOrderSource", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                ((IObjectContextAdapter)_sme).ObjectContext.Translate<tblPromotion>(rdr, "SalesManagerEntities.tblPromotion", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                ((IObjectContextAdapter)_sme).ObjectContext.Translate<tblOrderHeader>(rdr, "SalesManagerEntities.tblOrderHeader", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                ((IObjectContextAdapter)_sme).ObjectContext.Translate<tblProduct>(rdr, "SalesManagerEntities.tblProduct", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                ((IObjectContextAdapter)_sme).ObjectContext.Translate<tblOrderDetail>(rdr, "SalesManagerEntities.tblOrderDetail", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                ((IObjectContextAdapter)_sme).ObjectContext.Translate<tblSequence>(rdr, "SalesManagerEntities.tblSequence", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                ((IObjectContextAdapter)_sme).ObjectContext.Translate<tblSequenceOrder>(rdr, "SalesManagerEntities.tblSequenceOrder", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                ((IObjectContextAdapter)_sme).ObjectContext.Translate<tblSequenceStep>(rdr, "SalesManagerEntities.tblSequenceStep", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                ((IObjectContextAdapter)_sme).ObjectContext.Translate<tblSequenceStepProduct>(rdr, "SalesManagerEntities.tblSequenceStepProduct", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                ((IObjectContextAdapter)_sme).ObjectContext.Translate<tblSequenceOrderStep>(rdr, "SalesManagerEntities.tblSequenceOrderStep", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                ((IObjectContextAdapter)_sme).ObjectContext.Translate<tblSequenceOrderStepProduct>(rdr, "SalesManagerEntities.tblSequenceOrderStepProduct", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                ((IObjectContextAdapter)_sme).ObjectContext.Translate<tblRefund>(rdr, "SalesManagerEntities.tblRefund", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                ((IObjectContextAdapter)_sme).ObjectContext.Translate<tblRMAReturn>(rdr, "SalesManagerEntities.tblRMAReturn", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                ((IObjectContextAdapter)_sme).ObjectContext.Translate<tblReturnDetail>(rdr, "SalesManagerEntities.tblReturnDetail", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                ((IObjectContextAdapter)_sme).ObjectContext.Translate<tblTransaction>(rdr, "SalesManagerEntities.tblTransaction", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                ((IObjectContextAdapter)_sme).ObjectContext.Translate<tblAccount>(rdr, "SalesManagerEntities.tblAccount", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                ((IObjectContextAdapter)_sme).ObjectContext.Translate<tblTransactionDetail>(rdr, "SalesManagerEntities.tblTransactionDetail", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                ((IObjectContextAdapter)_sme).ObjectContext.Translate<tblMiscDef>(rdr, "SalesManagerEntities.tblMiscDef", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                ((IObjectContextAdapter)_sme).ObjectContext.Translate<tblInventory>(rdr, "SalesManagerEntities.tblInventory", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                ((IObjectContextAdapter)_sme).ObjectContext.Translate<tblInventoryTransaction>(rdr, "SalesManagerEntities.tblInventoryTransactions", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                ((IObjectContextAdapter)_sme).ObjectContext.Translate<tblInventoryTransactionDetail>(rdr, "SalesManagerEntities.tblInventoryTransactionDetail", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                ((IObjectContextAdapter)_sme).ObjectContext.Translate<tblPackage>(rdr, "SalesManagerEntities.tblPackages", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                ((IObjectContextAdapter)_sme).ObjectContext.Translate<tblOrderSourceID>(rdr, "SalesManagerEntities.tblOrderSourceID", MergeOption.AppendOnly).Count();
                rdr.NextResult();
                ((IObjectContextAdapter)_sme).ObjectContext.Translate<tblCustomerSourceID>(rdr, "SalesManagerEntities.tblCustomerSourceID", MergeOption.AppendOnly).Count();
                // Load the selcted customer from the database
                //IQueryable<tblCustomer> oq =
                //    from c in _sme.tblCustomer
                //        .Include("Person")
                //        .Include("Person.Address")
                //        .Include("Person.Address.Country")
                //        .Include("Person.Language")
                //        .Include("Person.ContactPoints")
                //        .Include("Person.ContactPoints.ContactPointType")
                //        .Include("OrderHeaders")
                //        .Include("OrderHeaders.ShipTo_Person")
                //        .Include("OrderHeaders.ShipTo_Address")
                //        .Include("OrderHeaders.ShipTo_Address.Country")
                //        .Include("OrderHeaders.HoldReason")
                //        .Include("OrderHeaders.OrderSource")
                //        .Include("OrderHeaders.OrderType")
                //        .Include("OrderHeaders.OrderStatus")
                //        .Include("OrderHeaders.OrderDetails")
                //        .Include("OrderHeaders.OrderDetails.Product")
                //        .Include("OrderHeaders.SequenceOrders.SequenceSteps.Sequence")
                //    where c.CustomerID == _customerID
                //    select c;
                ////string t = ((System.Data.Objects.ObjectQuery)oq).ToTraceString();
                //_customer = oq.First(f => f.CustomerID == _customerID);
            }
            else
                _customer = _sme.tblCustomer.Find(new object[] {_customerID});

            Load();
        }

        protected new void Load()
        {
            // Load the form from the database
            ((CustomerTab)TabContainer1.FindControl("TabPanelCustomer").FindControl("customerTab1")).Load(_sme, _customer); 
            ((OrderTab)TabContainer1.FindControl("TabPanelOrders").FindControl("orderTab1")).Load(_sme, _customer);
            ((SequenceTab)TabContainer1.FindControl("TabPanelSequences").FindControl("sequenceTab1")).Load(_sme, _customer);
            ((TransactionsTab)TabContainer1.FindControl("TabPanelTransactions").FindControl("transactionsTab1")).Load(_sme, 
                _sme.tblTransaction.Local.Where(p=>p.OrderHeader.CustomerID == _customer.CustomerID));
            ((CustomerSourceIDTab)TabContainer1.FindControl("tabPanelCustomerSourceID").FindControl("customerSourceIDsTab1")).Load(_sme,
                _sme.tblCustomerSourceID.Local.Where(p => p.CustomerID == _customer.CustomerID));
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Page.Validate();
            if (Page.IsValid)
            {
                _sme.SaveChanges();
            }
        }

        protected void btnHome_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx");
        }
    }
}
